<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Shop\Form;

use Admin\Data\Common;
use Laminas\Form\Form;
use User\Validator\UserNameValidator;

class HomeUserEditForm extends Form
{
    private $entityManager;
    private $userInfo;
    private $loginAccountEdit;

    public function __construct($entityManager = null, $userInfo = null, $name = 'home-user-edit-from', $options = [])
    {
        parent::__construct($name, $options);

        $this->setAttribute('method', 'post');

        $this->entityManager    = $entityManager;
        $this->userInfo         = $userInfo;
        $this->loginAccountEdit = Common::configValue('login_account_edit', 'customer');

        $this->addElements();
        $this->addInputFilter();
    }

    protected function addElements()
    {
        if ($this->loginAccountEdit == 1) {
            $this->add([
                'type'  => 'text',
                'name'  => 'userName',
                'attributes' => [
                    'id'    => 'userName'
                ]
            ]);
        }

        $this->add([
            'type'  => 'select',
            'name'  => 'userSex',
            'attributes'    => [
                'id'            => 'userSex'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'userBirthday',
            'attributes'    => [
                'id'            => 'userBirthday'
            ]
        ]);
    }

    protected function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        if ($this->loginAccountEdit == 1) {
            $inputFilter->add([
                'name'      => 'userName',
                'required'  => true,
                'filters'   => [
                    ['name' => 'StringTrim'],
                    ['name' => 'StripTags']
                ],
                'validators'=> [
                    [
                        'name'      => 'StringLength',
                        'options'   => [
                            'min'   => 1,
                            'max'   => 100
                        ]
                    ],
                    [
                        'name'      => UserNameValidator::class,
                        'options'    => [
                            'entityManager' => $this->entityManager,
                            'userInfo'      => $this->userInfo
                        ]
                    ]
                ]
            ]);
        }

        $inputFilter->add([
            'name'      => 'userSex',
            'required'  => true,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [1, 2, 3]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'userBirthday',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);
    }
}